package com.train.pattern.strategy.v1.pay;

public class Order {
    private String uid;
    private String orderId;
    private int amount;
    private double price;

    public Order(String uid, String orderId, int amount) {
        this.uid = uid;
        this.orderId = orderId;
        this.amount = amount;
        this.price=80.5;
    }

    /**
     * 付款
     * @param user  用户
     * @return
     */
    public MsgResult pay(User user) {
        double totalMoney=compute();//计算付款总金额
        Account account=user.getAccount();
        StringBuilder sb = new StringBuilder("欢迎使用");
        if ("AliPay".equals(account.getPayWay())) { //阿里支付
            sb.append("支付宝\n").append("本次交易金额为").append(totalMoney).append(",开始扣款");
            System.out.println(sb.toString());
            //余额是否足够
            if (account.getBalance() < totalMoney) {
                return new MsgResult(500, "支付失败", "余额不足");
            } else {
                return new MsgResult(200, "支付成功", "支付金额" + totalMoney);
            }

        } else if ("WechatPay".equals(account.getPayWay())) { //微信支付
            sb.append("微信支付").append("\n 本次交易金额为").append(totalMoney).append(",开始扣款");
            System.out.println(sb.toString());
            //余额是否足够
            if (account.getBalance() < totalMoney) {
                return new MsgResult(500, "支付失败", "余额不足");
            } else {
                return new MsgResult(200, "支付成功", "支付金额" + totalMoney);
            }

        } else if ("UnionPay".equals(account.getPayWay())) { //银联支付
            sb.append("银联支付").append("\n 本次交易金额为").append(totalMoney).append(",开始扣款");
            System.out.println(sb.toString());

            //余额是否足够
            if (account.getBalance() < totalMoney) {
                return new MsgResult(500, "支付失败", "余额不足");
            } else {
                return new MsgResult(200, "支付成功", "支付金额" + totalMoney);
            }

        } else if ("JdPay".equals(account.getPayWay())) { //京东白条
            sb.append("京东白条").append("\n 本次交易金额为").append(totalMoney).append(",开始扣款");
            System.out.println(sb.toString());

            //余额是否足够
            if (account.getBalance() < totalMoney) {
                return new MsgResult(500, "支付失败", "余额不足");
            } else {
                return new MsgResult(200, "支付成功", "支付金额" + totalMoney);
            }

        } else {
            sb.append("支付宝").append("\n 本次交易金额为").append(totalMoney).append(",开始扣款");
            System.out.println(sb.toString());

            //余额是否足够
            if (account.getBalance() < totalMoney) {
                return new MsgResult(500, "支付失败", "余额不足");
            } else {
                return new MsgResult(200, "支付成功", "支付金额" + totalMoney);
            }
        }
    }

    /**
     * 计算总价
     * @return
     */
    private double compute(){
        double totalMoney=this.amount * this.price;
        return totalMoney;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
